﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Meeting.Model;
using Newtonsoft.Json;
using System.Configuration;

namespace Meeting.Web.ashx
{
    /// <summary>
    /// Meeting 的摘要说明
    /// </summary>
    public class MeetingHandler : IHttpHandler
    {
        string methodName = "";
        bool jsonP = false;
        BLL.MeetingBLL bll = new BLL.MeetingBLL();

        public void ProcessRequest(HttpContext context)
        {
            String header = "{0}";
            try
            {
                String cb = context.Request.Params["callback"]; //  
                //传过来的 callback 参数 假如 Ext.data.JsonP.callback  
                if (cb != null)
                {
                    jsonP = true;
                    context.Response.ContentType = "text/javascript";
                }
                else
                {
                    context.Response.ContentType = "application/x-json;charset=utf-8";
                }

                header = "";
                if (jsonP)
                {
                    header = cb + "({0})";
                }
                else
                {
                    header = "{0}";
                }

                //context.Response.ContentType = "application/json;charset=utf-8";
                //context.Response.ContentType = "text/plain";
                methodName = context.Request.Params["method"];
                int id;
                try
                {
                    id = Convert.ToInt32(context.Request.Params["id"]);
                }
                catch { id = 0; }

                int category = 0;

                switch (context.Request.Params["category"])
                {
                    case "dz":
                        category = 1;
                        break;
                    case "bg":
                        category = 2;
                        break;
                    case "fjz":
                        category = 3;
                        break;
                    case "qt":
                        category = 4;
                        break;
                    default:
                        category = 0;
                        break;
                }

                switch (methodName.ToLower())
                {
                    case "getlast":   //获取json数据                        
                        //int start, limit;
                        //try
                        //{
                        //    start = Convert.ToInt32(context.Request.Params["start"]);
                        //    limit = Convert.ToInt32(context.Request.Params["limit"]);
                        //}
                        //catch
                        //{
                        //    start = 1;
                        //    limit = 10;
                        //}
                        //查询条件

                        string filter = " me.EmployeeId = '" + context.Request.Params["employeeId"] + "' ";
                        if (category != 0)
                        {
                            filter += " and m.CategoryId= " + category;
                        }
                        string title = context.Request.Params["title"];
                        DateTime startDate = Convert.ToDateTime(context.Request.Params["startDate"]);
                        DateTime endDate = Convert.ToDateTime(context.Request.Params["endDate"]);
                        if (!string.IsNullOrEmpty(title))
                        {
                            filter += " and t.Title like '%" + title + "%' ";
                        }
                        filter += " and m.StartDate >= '" + startDate.ToShortDateString() + "' and m.StartDate < '" + endDate.AddDays(1).ToShortDateString() + "'";

                        //context.Response.Write(string.Format(header, bll.GetMeetingPage(start, limit, filter)));
                        context.Response.Write(string.Format(header, bll.GetMeetingHistory(filter)));
                        break;
                    case "getmy":
                        string employeeId = context.Request.Params["employeeid"];
                        context.Response.Write(string.Format(header, bll.GetMyMeeting(employeeId)));
                        break;
                    case "getlastid":   //获取json数据                        
                        context.Response.Write(string.Format(header, bll.GetLastMeetingInfo(category, context.Request.Params["userid"])));
                        break;
                    case "getlastten":   //获取json数据                      
                        context.Response.Write(string.Format(header, bll.GetLastTenMeeting(category, id)));
                        break;
                    case "getcomment":
                        context.Response.Write(string.Format(header, bll.GetComment(context.Request.Params["id"])));
                        break;
                    case "getcommentby":
                        Guid docId = new Guid(context.Request.Params["docId"]);
                        context.Response.Write(string.Format(header, bll.GetCommentByDoc(context.Request.Params["id"],docId)));
                        break;
                    case "delcomment":
                        Guid commentId = new Guid(context.Request.Params["commentId"]);
                        int meetingId = Convert.ToInt32(context.Request.Params["meetingId"]);
                        string path = context.Request.Params["path"];
                        //首先从服务器删除文件                        
                        //文件路径获取                         
                        string rootDir = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["UploadPath"]);
                        DirectoryInfo info = new DirectoryInfo(rootDir + "/comment/" + path + "/" + meetingId.ToString() + "/" + commentId.ToString() + "/");
                        info.Delete(true);
                        context.Response.Write(string.Format(header, bll.DeleteComment(commentId)));
                        break;
                    case "getcommentbymeeting":
                        //获取当前会议的批注
                        //用户名和会议ID
                        employeeId = context.Request.Params["employeeid"];
                        meetingId = Convert.ToInt32(context.Request.Params["meetingid"]);
                        context.Response.Write(string.Format(header, bll.GetCommentByMeeting(employeeId, meetingId)));
                        break;
                    case "getcommentbyhistory":
                        //获取用户当前会议之外的历史批注
                        //用户名和会议ID
                        employeeId = context.Request.Params["employeeId"];
                        meetingId = Convert.ToInt32(context.Request.Params["meetingId"]);
                        context.Response.Write(string.Format(header, bll.GetCommentByHistory(employeeId, meetingId)));
                        break;
                    //case "getmeetingby":
                    //    context.Response.Write(string.Format(header, bll.GetMeetingBy(id)));
                    //    break;
                    case "gettopicby":
                        //var obj = string.Format(header, bll.GetTopicBy(id));
                        context.Response.Write(string.Format(header, bll.GetTopicBy(id)));
                        break;
                    case "getdocby":
                        Guid topicId = new Guid(context.Request.Params["id"]);
                        context.Response.Write(string.Format(header, bll.GetDocBy(topicId)));
                        break;

                    case "getexcepttopicby":
                        //var obj = string.Format(header, bll.GetTopicBy(id));
                        topicId = new Guid(context.Request.Params["topicId"]);
                        context.Response.Write(string.Format(header, bll.GetExceptTopicBy(id, topicId)));
                        break;
                    case "getexceptdocby":
                        topicId = new Guid(context.Request.Params["topicId"]);
                        docId = Guid.Empty;
                        try
                        {
                            docId = new Guid(context.Request.Params["docId"]);
                        }
                        catch { }

                        context.Response.Write(string.Format(header, bll.GetExceptDocBy(topicId, docId)));
                        break;
                    case "getplateby":
                        topicId = new Guid(context.Request.Params["topicId"]);
                        context.Response.Write(string.Format(header, bll.GetPlatesBy(topicId)));
                        break;
                    case "getemployee":
                        context.Response.Write(string.Format(header, bll.GetMeetingEmployees(id)));
                        break;
                    case "getsignemployee":
                        context.Response.Write(string.Format(header, bll.GetMeetingSignEmployees(id)));
                        break;
                    case "getmeetingcategory":
                        context.Response.Write(string.Format(header, bll.GetMeetingCategory()));
                        break;
                    case "getsummary":
                        meetingId = Convert.ToInt32(context.Request.Params["meetingId"]);
                        context.Response.Write(string.Format(header, bll.GetMeetingSummaries(meetingId)));
                        break;
                    case "getsummaryby":
                        Guid summaryId = new Guid(context.Request.Params["summaryId"]);
                        context.Response.Write(string.Format(header, bll.GetSummaryBy(summaryId)));
                        break;
                    case "delsummary":
                        summaryId = new Guid(context.Request.Params["summaryId"]);
                        //首先从服务器删除文件
                        //文件路径获取
                        Model.MeetingSummary summary = bll.GetSummary(summaryId);
                        rootDir = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["UploadPath"]);
                        File.Delete(rootDir + "/" + summary.FilePath);
                        info = new DirectoryInfo(rootDir + "/" + summary.ImageDir + "/");
                        info.Delete(true);
                        context.Response.Write(string.Format(header, bll.DeleteSummary(summaryId)));
                        break;
                    case "requestapprove":
                        var jSetting = new JsonSerializerSettings();
                        jSetting.NullValueHandling = NullValueHandling.Ignore;
                        summary = JsonConvert.DeserializeObject<Model.MeetingSummary>(context.Request["param"], jSetting);
                        context.Response.Write(string.Format(header, bll.RequestApprove(summary)));
                        break;
                    case "submitcomment":
                        jSetting = new JsonSerializerSettings();
                        jSetting.NullValueHandling = NullValueHandling.Ignore;
                        Model.SummaryApprove approve = JsonConvert.DeserializeObject<Model.SummaryApprove>(context.Request["param"], jSetting);
                        context.Response.Write(string.Format(header, bll.SubmitApprove(approve)));
                        break;
                    case "getunsign":
                        employeeId = context.Request.Params["employeeId"];
                        context.Response.Write(string.Format(header, bll.GetUnSignSummaryBy(employeeId)));
                        break;
                    case "getsign":
                        employeeId = context.Request.Params["employeeId"];
                        context.Response.Write(string.Format(header, bll.GetSignedSummaryBy(employeeId)));
                        break;
                    case "getmyvideo":
                        employeeId = context.Request.Params["employeeId"];
                        context.Response.Write(string.Format(header, bll.GetMyVideoMeeting(employeeId)));
                        break;
                    case "delvideo":
                        Guid videoId = new Guid(context.Request.Params["videoId"]);
                        context.Response.Write(string.Format(header, bll.DeleteVideoMeeting(videoId)));
                        break;
                    case "getoauser":
                        BLL.Employee emp = new BLL.Employee();
                        context.Response.Write(string.Format(header, emp.GetOaAllUser()));
                        break;
                    case "gettempvideo":
                        context.Response.Write(string.Format(header, bll.GetTempVideoRoom()));
                        break;
                    case "savevideomeeting":
                        jSetting = new JsonSerializerSettings();
                        jSetting.NullValueHandling = NullValueHandling.Ignore;
                        Model.VideoMeeting model = JsonConvert.DeserializeObject<Model.VideoMeeting>(context.Request.Params["meeting"], jSetting);
                        List<Model.VideoMeetingEmployee> employees = JsonConvert.DeserializeObject<List<Model.VideoMeetingEmployee>>(context.Request.Params["employees"], jSetting);
                        context.Response.Write(string.Format(header, bll.SaveVideoMeeting(model, employees)));
                        break;

                    //case "inserttopicresult":
                    //    context.Response.Write(string.Format(header, bll.InsertTopicResult(context.Request.Params["param"])));
                    //    break;
                    //case "checktopicvote":
                    //    int employeeId = 0;
                    //    int meetingId = 0;
                    //    try
                    //    {
                    //        employeeId = Convert.ToInt32(context.Request.Params["employeeid"]);
                    //        meetingId = Convert.ToInt32(context.Request.Params["meetingid"]);
                    //    }
                    //    catch { }
                    //    context.Response.Write(string.Format(header, bll.CheckTopicVote(employeeId, meetingId)));
                    //    break;
                    //case "login":
                    //    string name = "";
                    //    string password = "";
                    //    try
                    //    {
                    //        name = context.Request.Params["name"];
                    //        password = context.Request.Params["password"];
                    //    }
                    //    catch { }
                    //    context.Response.Write(string.Format(header, bll.Login(name, password)));
                    //    break;
                }
            }
            catch (Exception ex)
            {
                JsonResponse response = new JsonResponse();
                response.success = false;
                response.Message = ex.Message;
                context.Response.Write(string.Format(header, JsonConvert.SerializeObject(response)));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}